<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li class="active"><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 进程">1. 进程</a><ul><li><a href="#t11.1 进程对象属性">1.1 进程对象属性</a></li><li><a href="#t21.2 memoryUsage方法">1.2 memoryUsage方法</a></li><li><a href="#t31.3 nextTick方法">1.3 nextTick方法</a></li><li><a href="#t41.4 chdir">1.4 chdir</a></li><li><a href="#t51.5 cwd 方法">1.5 cwd 方法</a></li><li><a href="#t61.6 chdir 方法">1.6 chdir 方法</a></li><li><a href="#t71.7 exit 方法">1.7 exit 方法</a></li><li><a href="#t81.8 kill方法">1.8 kill方法</a></li><li><a href="#t91.9 uptime">1.9 uptime</a></li><li><a href="#t101.10 hrtime">1.10 hrtime</a></li><li><a href="#t111.11 exit事件">1.11 exit事件</a></li><li><a href="#t121.12 uncaughtException事件">1.12 uncaughtException事件</a></li><li><a href="#t131.13 信号事件">1.13 信号事件</a></li></ul></li><li><a href="#t142. 子进程">2. 子进程</a><ul><li><a href="#t152.1 spawn">2.1 spawn</a><ul><li><a href="#t162.1.1 语法">2.1.1 语法</a></li><li><a href="#t172.1.2 close">2.1.2 close</a></li><li><a href="#t182.1.3 exit">2.1.3 exit</a></li><li><a href="#t192.1.4 error">2.1.4 error</a></li><li><a href="#t202.1.5 kill">2.1.5 kill</a></li><li><a href="#t212.1.6 案例">2.1.6 案例</a></li><li><a href="#t222.1.7 detached">2.1.7 detached</a></li></ul></li><li><a href="#t232.2 fork开启子进程">2.2 fork开启子进程</a><ul><li><a href="#t242.2.1 发送消息">2.2.1 发送消息</a></li><li><a href="#t252.2.2 silent">2.2.2 silent</a></li><li><a href="#t262.2.3 子进程与父进程共享HTTP服务器">2.2.3 子进程与父进程共享HTTP服务器</a></li><li><a href="#t272.2.4 子进程与父进程共享socket对象">2.2.4 子进程与父进程共享socket对象</a></li></ul></li><li><a href="#t282.3 exec开启子进程">2.3 exec开启子进程</a></li><li><a href="#t292.4 execFile开启子进程">2.4 execFile开启子进程</a></li></ul></li><li><a href="#t303. cluster">3. cluster</a><ul><li><a href="#t313.1 fork方法创建work对象">3.1 fork方法创建work对象</a><ul><li><a href="#t323.1.1 获取所有的worker">3.1.1 获取所有的worker</a></li><li><a href="#t333.1.2 获取当前的worker和id">3.1.2 获取当前的worker和id</a></li><li><a href="#t343.1.3 服务器">3.1.3 服务器</a></li><li><a href="#t353.1.4 fork事件">3.1.4 fork事件</a></li><li><a href="#t363.1.5 online事件">3.1.5 online事件</a></li><li><a href="#t373.1.6 listening">3.1.6 listening</a></li><li><a href="#t383.1.7 setupMaster">3.1.7 setupMaster</a></li><li><a href="#t393.1.8 在子进程里运行服务器">3.1.8 在子进程里运行服务器</a></li><li><a href="#t403.1.9 在子进程使用单独的输出">3.1.9 在子进程使用单独的输出</a></li></ul></li><li><a href="#t413.2 worker对象">3.2 worker对象</a><ul><li><a href="#t423.2.1 online">3.2.1 online</a></li><li><a href="#t433.2.2 send">3.2.2 send</a></li><li><a href="#t443.2.3 共享socket">3.2.3 共享socket</a></li><li><a href="#t453.2.4 kill">3.2.4 kill</a></li><li><a href="#t463.2.5 exit">3.2.5 exit</a></li><li><a href="#t473.2.6 disconnect">3.2.6 disconnect</a></li></ul></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x8FDB;&#x7A0B;">1. &#x8FDB;&#x7A0B; <a href="#t01. &#x8FDB;&#x7A0B;"> # </a></h2>
<ul>
<li>&#x5728;Node.js&#x4E2D;&#x6BCF;&#x4E2A;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x4F7F;&#x7528;<code>process</code>&#x5BF9;&#x8C61;&#x4EE3;&#x8868;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;,&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B83;&#x6765;&#x83B7;&#x53D6;Node.jsy&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4EE5;&#x53CA;&#x8FD0;&#x884C;&#x8BE5;&#x7A0B;&#x5E8F;&#x7684;&#x7528;&#x6237;&#x3001;&#x73AF;&#x5883;&#x7B49;&#x5404;&#x79CD;&#x4FE1;&#x606F;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x4E8B;&#x4EF6;&#x3002;</li>
</ul>
<h3 id="t11.1 &#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;">1.1 &#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x5C5E;&#x6027; <a href="#t11.1 &#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;"> # </a></h3>
<ul>
<li>execPath &#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;,&#x5982; <code>/usr/local/bin/node</code></li>
<li>version &#x7248;&#x672C;&#x53F7;</li>
<li>versions &#x4F9D;&#x8D56;&#x5E93;&#x7684;&#x7248;&#x672C;&#x53F7;</li>
<li>platform &#x8FD0;&#x884C;&#x5E73;&#x53F0;&#x3002; &#x5982; darwin&#x3001;freebsd&#x3001;linux&#x3001;sunos&#x3001;win32</li>
<li>stdin &#x6807;&#x51C6;&#x8F93;&#x5165;&#x6D41;&#x53EF;&#x8BFB;&#x6D41;&#xFF0C;&#x9ED8;&#x8BA4;&#x6682;&#x505C;&#x72B6;&#x6001;</li>
<li>stdout &#x6807;&#x51C6;&#x8F93;&#x51FA;&#x53EF;&#x5199;&#x6D41;&#xFF0C;&#x540C;&#x6B65;&#x64CD;&#x4F5C; </li>
<li>stderr &#x9519;&#x8BEF;&#x8F93;&#x51FA;&#x53EF;&#x5199;&#x6D41;&#xFF0C;&#x540C;&#x6B65;&#x64CD;&#x4F5C;</li>
<li>argv &#x5C5E;&#x6027;&#x503C;&#x4E3A;&#x6570;&#x7EC4;</li>
<li>env &#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x73AF;&#x5883;&#x4FE1;&#x606F;</li>
<li>pid &#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x8FDB;&#x7A0B;ID</li>
<li>title &#x7A97;&#x53E3;&#x6807;&#x9898;</li>
<li>arch &#x5904;&#x7406;&#x5668;&#x67B6;&#x6784; arm ia32 x64</li>
</ul>
<pre><code class="lang-javascript">process.stdin.resume();
process.stdin.on(<span class="hljs-string">&apos;data&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">chunk</span>)</span>{
  process.stdout.write(<span class="hljs-string">`&#x8FDB;&#x7A0B;&#x63A5;&#x6536;&#x5230;&#x6570;&#x636E;: `</span>+chunk);
});
</code></pre>
<pre><code class="lang-javascript">process.argv.forEach(<span class="hljs-function">(<span class="hljs-params">val,index,ary</span>)=&gt;</span> <span class="hljs-built_in">console</span>.log(index,val));
</code></pre>
<h3 id="t21.2 memoryUsage&#x65B9;&#x6CD5;">1.2 memoryUsage&#x65B9;&#x6CD5; <a href="#t21.2 memoryUsage&#x65B9;&#x6CD5;"> # </a></h3>
<pre><code class="lang-javascript">process.memoryUsage()
</code></pre>
<p><img src="http://img.zhufengpeixun.cn/stack.png" alt="stack"></p>
<ul>
<li>rss&#xFF08;resident set size&#xFF09;&#xFF1A;&#x6240;&#x6709;&#x5185;&#x5B58;&#x5360;&#x7528;&#xFF0C;&#x5305;&#x62EC;&#x6307;&#x4EE4;&#x533A;&#x548C;&#x5806;&#x6808;&#x3002;</li>
<li>heapTotal&#xFF1A;&quot;&#x5806;&quot;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#xFF0C;&#x5305;&#x62EC;&#x7528;&#x5230;&#x7684;&#x548C;&#x6CA1;&#x7528;&#x5230;&#x7684;&#x3002;</li>
<li>heapUsed&#xFF1A;&#x7528;&#x5230;&#x7684;&#x5806;&#x7684;&#x90E8;&#x5206;&#x3002;</li>
<li>external&#xFF1A; V8 &#x5F15;&#x64CE;&#x5185;&#x90E8;&#x7684; C++ &#x5BF9;&#x8C61;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#x3002;</li>
</ul>
<h3 id="t31.3 nextTick&#x65B9;&#x6CD5;">1.3 nextTick&#x65B9;&#x6CD5; <a href="#t31.3 nextTick&#x65B9;&#x6CD5;"> # </a></h3>
<p>nextTick&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5C06;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x63A8;&#x8FDF;&#x5230;&#x4EE3;&#x7801;&#x4E2D;&#x6240;&#x4E66;&#x5199;&#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x6216;&#x5F02;&#x6B65;&#x65B9;&#x6CD5;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5F00;&#x59CB;&#x6267;&#x884C;&#x524D;&#x8C03;&#x7528;</p>
<h3 id="t41.4 chdir">1.4 chdir <a href="#t41.4 chdir"> # </a></h3>
<p>chdir&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x4FEE;&#x6539;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x5F53;&#x524D;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;&#xFF0C;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x5982;&#x4E0B; </p>
<pre><code class="lang-javascript">process.chdir(directory);
</code></pre>
<h3 id="t51.5 cwd &#x65B9;&#x6CD5;">1.5 cwd &#x65B9;&#x6CD5; <a href="#t51.5 cwd &#x65B9;&#x6CD5;"> # </a></h3>
<p>cwd&#x65B9;&#x6CD5;&#x7528;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#xFF0C;&#x4E0D;&#x4F7F;&#x7528;&#x4EFB;&#x4F55;&#x53C2;&#x6570;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(process.cwd());
</code></pre>
<h3 id="t61.6 chdir &#x65B9;&#x6CD5;">1.6 chdir &#x65B9;&#x6CD5; <a href="#t61.6 chdir &#x65B9;&#x6CD5;"> # </a></h3>
<p>&#x6539;&#x53D8;&#x5F53;&#x524D;&#x7684;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;</p>
<pre><code class="lang-javascript">console.log(`&#x5F53;&#x524D;&#x76EE;&#x5F55;: ${process.cwd()}`);
process.chdir(&apos;..);
console.log(`&#x4E0A;&#x5C42;&#x76EE;&#x5F55;: ${process.cwd()});
</code></pre>
<h3 id="t71.7 exit &#x65B9;&#x6CD5;">1.7 exit &#x65B9;&#x6CD5; <a href="#t71.7 exit &#x65B9;&#x6CD5;"> # </a></h3>
<p>&#x9000;&#x51FA;&#x8FD0;&#x884C;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x8FDB;&#x7A0B;</p>
<pre><code class="lang-javascript">process.exit(<span class="hljs-number">0</span>);
</code></pre>
<h3 id="t81.8 kill&#x65B9;&#x6CD5;">1.8 kill&#x65B9;&#x6CD5; <a href="#t81.8 kill&#x65B9;&#x6CD5;"> # </a></h3>
<p>&#x7528;&#x4E8E;&#x5411;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x4E00;&#x4E2A;&#x4FE1;&#x53F7;</p>
<ul>
<li>SIGINT &#x7A0B;&#x5E8F;&#x7EC8;&#x6B62;(interrupt)&#x4FE1;&#x53F7;, &#x5728;&#x7528;&#x6237;&#x952E;&#x5165;INTR&#x5B57;&#x7B26;(&#x901A;&#x5E38;&#x662F;Ctrl-C)&#x65F6;&#x53D1;&#x51FA;&#xFF0C;&#x7528;&#x4E8E;&#x901A;&#x77E5;&#x524D;&#x53F0;&#x8FDB;&#x7A0B;&#x7EC4;&#x7EC8;&#x6B62;&#x8FDB;&#x7A0B;&#x3002;</li>
<li>SIGTERM &#x7A0B;&#x5E8F;&#x7ED3;&#x675F;(terminate)&#x4FE1;&#x53F7;, &#x8BE5;&#x4FE1;&#x53F7;&#x53EF;&#x4EE5;&#x88AB;&#x963B;&#x585E;&#x548C;&#x5904;&#x7406;&#x3002;&#x901A;&#x5E38;&#x7528;&#x6765;&#x8981;&#x6C42;&#x7A0B;&#x5E8F;&#x81EA;&#x5DF1;&#x6B63;&#x5E38;&#x9000;&#x51FA;&#xFF0C;shell&#x547D;&#x4EE4;kill&#x7F3A;&#x7701;&#x4EA7;&#x751F;&#x8FD9;&#x4E2A;&#x4FE1;&#x53F7;</li>
</ul>
<pre><code class="lang-javascript">process.kill(pid,[signal]);
</code></pre>
<ul>
<li>pid&#x662F;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#xFF0C;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x9700;&#x8981;&#x63A5;&#x6536;&#x4FE1;&#x53F7;&#x7684;&#x8FDB;&#x7A0B;ID</li>
<li>signal  &#x53D1;&#x9001;&#x7684;&#x4FE1;&#x53F7;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; SIGTERM</li>
</ul>
<h3 id="t91.9 uptime">1.9 uptime <a href="#t91.9 uptime"> # </a></h3>
<p>&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x7A0B;&#x5E8F;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x95F4;</p>
<pre><code class="lang-javascript">process.uptime()
</code></pre>
<h3 id="t101.10 hrtime">1.10 hrtime <a href="#t101.10 hrtime"> # </a></h3>
<p>&#x6D4B;&#x8BD5;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x6BB5;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x95F4;,&#x8FD4;&#x56DE;&#x4E24;&#x4E2A;&#x65F6;&#x95F4;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x5355;&#x4F4D;&#x662F;&#x79D2;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x5355;&#x4F4D;&#x662F;&#x7EB3;&#x79D2;</p>
<pre><code class="lang-javascript">let fs = require(&apos;fs);
let time = process.hrtime();
let data = fs.readFileSync(&apos;index.txt&apos;);
let diff = process.hrtime(time);
console.log(`&#x8BFB;&#x6587;&#x4EF6;&#x64CD;&#x4F5C;&#x8017;&#x8D39;&#x7684;%d&#x79D2;`,diff[0]);
</code></pre>
<h3 id="t111.11 exit&#x4E8B;&#x4EF6;">1.11 exit&#x4E8B;&#x4EF6; <a href="#t111.11 exit&#x4E8B;&#x4EF6;"> # </a></h3>
<p>&#x5F53;&#x8FD0;&#x884C;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#x89E6;&#x53D1;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;exit&#x4E8B;&#x4EF6;&#x3002;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6307;&#x5B9A;&#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x6765;&#x6307;&#x5B9A;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#x6240;&#x6267;&#x884C;&#x7684;&#x5904;&#x7406;&#x3002;</p>
<pre><code class="lang-javascript">process.on(&apos;exit&apos;,function(){
    console.log(&apos;Node.js&#x8FDB;&#x7A0B;&#x88AB;&#x63A8;&#x51FA;);
});
process.exit();
</code></pre>
<h3 id="t121.12 uncaughtException&#x4E8B;&#x4EF6;">1.12 uncaughtException&#x4E8B;&#x4EF6; <a href="#t121.12 uncaughtException&#x4E8B;&#x4EF6;"> # </a></h3>
<p>&#x5F53;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x672A;&#x88AB;&#x6355;&#x83B7;&#x7684;&#x5F02;&#x5E38;&#x65F6;&#x89E6;&#x53D1;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;uncaughtException&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascript">process.on(<span class="hljs-string">&apos;uncaughtException&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err</span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6355;&#x83B7;&#x5230;&#x4E00;&#x4E2A;&#x672A;&#x88AB;&#x5904;&#x7406;&#x7684;&#x9519;&#x8BEF;:&apos;</span>,err);
});
notExist();
</code></pre>
<h3 id="t131.13 &#x4FE1;&#x53F7;&#x4E8B;&#x4EF6;">1.13 &#x4FE1;&#x53F7;&#x4E8B;&#x4EF6; <a href="#t131.13 &#x4FE1;&#x53F7;&#x4E8B;&#x4EF6;"> # </a></h3>
<pre><code class="lang-javascript">process.stdin.resume();
process.on(<span class="hljs-string">&apos;SIGINT&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x63A5;&#x6536;&#x5230;SIGINT&#x4FE1;&#x53F7;&apos;</span>);
});
</code></pre>
<h2 id="t142. &#x5B50;&#x8FDB;&#x7A0B;">2. &#x5B50;&#x8FDB;&#x7A0B; <a href="#t142. &#x5B50;&#x8FDB;&#x7A0B;"> # </a></h2>
<ul>
<li>&#x5728;Node.js&#x4E2D;&#xFF0C;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x6240;&#x6709;&#x64CD;&#x4F5C;&#xFF0C;&#x5982;&#x679C;&#x67D0;&#x4E2A;&#x64CD;&#x4F5C;&#x9700;&#x8981;&#x5927;&#x91CF;&#x6D88;&#x8017;CPU&#x8D44;&#x6E90;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x540E;&#x7EED;&#x64CD;&#x4F5C;&#x90FD;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x3002;</li>
<li>&#x5728;Node.js&#x4E2D;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;<code>child_process</code>&#x6A21;&#x5757;,&#x901A;&#x8FC7;&#x5B83;&#x53EF;&#x4EE5;&#x5F00;&#x542F;&#x591A;&#x4E2A;&#x5B50;&#x8FDB;&#x7A0B;&#xFF0C;&#x5728;&#x591A;&#x4E2A;&#x5B50;&#x8FDB;&#x7A0B;&#x4E4B;&#x95F4;&#x53EF;&#x4EE5;&#x5171;&#x4EAB;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x4E92;&#x76F8;&#x901A;&#x4FE1;&#x6765;&#x5B9E;&#x73B0;&#x4FE1;&#x606F;&#x7684;&#x4EA4;&#x6362;&#x3002;<h3 id="t152.1 spawn">2.1 spawn <a href="#t152.1 spawn"> # </a></h3>
<h4 id="t162.1.1 &#x8BED;&#x6CD5;">2.1.1 &#x8BED;&#x6CD5; <a href="#t162.1.1 &#x8BED;&#x6CD5;"> # </a></h4>
<pre><code class="lang-javascript">child_process.spawn(command,[args],[options]);
</code></pre>
</li>
<li>command &#x5FC5;&#x987B;&#x6307;&#x5B9A;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x6307;&#x5B9A;&#x9700;&#x8981;&#x6267;&#x884C;&#x7684;&#x547D;&#x4EE4;</li>
<li>args &#x6570;&#x7EC4;&#xFF0C;&#x5B58;&#x653E;&#x4E86;&#x6240;&#x6709;&#x8FD0;&#x884C;&#x8BE5;&#x547D;&#x4EE4;&#x9700;&#x8981;&#x7684;&#x53C2;&#x6570;</li>
<li>options &#x53C2;&#x6570;&#x4E3A;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;&#x65F6;&#x4F7F;&#x7528;&#x7684;&#x9009;&#x9879;<ul>
<li>cwd &#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;</li>
<li>env &#x73AF;&#x5883;&#x53D8;&#x91CF;</li>
<li>detached &#x5982;&#x679C;&#x4E3A;true,&#x8BE5;&#x5B50;&#x8FDB;&#x7A0B;&#x9B4F;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x7EC4;&#x4E2D;&#x7684;&#x9886;&#x5934;&#x8FDB;&#x7A0B;&#xFF0C;&#x5F53;&#x7236;&#x8FDB;&#x7A0B;&#x4E0D;&#x5B58;&#x5728;&#x65F6;&#x4E5F;&#x53EF;&#x4EE5;&#x72EC;&#x7ACB;&#x5B58;&#x5728;</li>
<li>stdio &#x4E09;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x6570;&#x7EC4;&#xFF0C;&#x8BBE;&#x7F6E;&#x6807;&#x51C6;&#x8F93;&#x5165;/&#x8F93;&#x51FA;<ul>
<li>pipe &#x5728;&#x7236;&#x8FDB;&#x7A0B;&#x548C;&#x5B50;&#x8FDB;&#x7A0B;&#x4E4B;&#x95F4;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7BA1;&#x9053;&#xFF0C;&#x7236;&#x8FDB;&#x7A0B;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;stdio[0]&#x8BBF;&#x95EE;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x5165;&#xFF0C;&#x901A;&#x8FC7;stdio[1]&#x8BBF;&#x95EE;&#x6807;&#x51C6;&#x8F93;&#x51FA;,stdio[2]&#x8BBF;&#x95EE;&#x9519;&#x8BEF;&#x8F93;&#x51FA;</li>
<li>ipc &#x5728;&#x7236;&#x8FDB;&#x7A0B;&#x548C;&#x5B50;&#x8FDB;&#x7A0B;&#x4E4B;&#x95F4;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4E13;&#x7528;&#x4E0E;&#x4F20;&#x9012;&#x6D88;&#x606F;&#x7684;IPC&#x901A;&#x9053;&#x3002;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;send&#x65B9;&#x6CD5;&#x5411;&#x5B50;&#x8FDB;&#x7A0B;&#x53D1;&#x6D88;&#x606F;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x4F1A;&#x89E6;&#x53D1;<code>message</code>&#x4E8B;&#x4EF6;</li>
<li>ignore &#x6307;&#x5B9A;&#x4E0D;&#x4E3A;&#x5B50;&#x8FDB;&#x7A0B;&#x8BBE;&#x7F6E;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x3002;&#x8FD9;&#x6837;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x3001;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x548C;&#x9519;&#x8BEF;&#x8F93;&#x51FA;&#x88AB;&#x5FFD;&#x7565;</li>
<li>stream &#x5B50;&#x8FDB;&#x7A0B;&#x548C;&#x7236;&#x8FDB;&#x7A0B;&#x5171;&#x4EAB;&#x4E00;&#x4E2A;&#x7EC8;&#x7AEF;&#x8BBE;&#x5907;&#x3001;&#x6587;&#x4EF6;&#x3001;&#x7AEF;&#x53E3;&#x6216;&#x7BA1;&#x9053;</li>
<li>&#x6B63;&#x6574;&#x6570;&#x503C; &#x548C;&#x5171;&#x4EAB;&#x4E00;&#x4E2A;steam&#x662F;&#x4E00;&#x6837;&#x7684;</li>
<li>null&#x6216;undefined &#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x521B;&#x5EFA;&#x4E0E;&#x7236;&#x8FDB;&#x7A0B;&#x76F8;&#x8FDE;&#x7684;&#x7BA1;&#x9053;</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;stdin,stdout,stderr&#x5BFC;&#x5411;&#x4E86;ChildProcess&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;child.stdin,child.stdout,child.stderr&#x6D41;,</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> spawn = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>).spawn;
sapwn(<span class="hljs-string">&apos;prg&apos;</span>,[],{<span class="hljs-attr">stdio</span>:[<span class="hljs-string">&apos;pipe&apos;</span>,<span class="hljs-string">&apos;pipe&apos;</span>,process.stderr]});
</code></pre>
<ul>
<li>ignore [&apos;ignore&apos;,&apos;ignore&apos;,&apos;ignore&apos;] &#x5168;&#x90E8;&#x5FFD;&#x7565;</li>
<li>pipe [&apos;pipe&apos;,&apos;pipe&apos;,&apos;pipe&apos;] &#x901A;&#x8FC7;&#x7BA1;&#x9053;&#x8FDE;&#x63A5;</li>
<li>inherit [process.stdin,process.stdout,process.stderr]&#x6216;[0,1,2] &#x548C;&#x7236;&#x8FDB;&#x7A0B;&#x5171;&#x4EAB;&#x8F93;&#x5165;&#x8F93;&#x51FA;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> spawn = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>).spawn;
spawn(<span class="hljs-string">&apos;prg&apos;</span>,[],{<span class="hljs-attr">stdio</span>:<span class="hljs-string">&apos;inherit&apos;</span>});
</code></pre>
<ul>
<li>spawn&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x9690;&#x5F0F;&#x521B;&#x5EFA;&#x7684;&#x4EE3;&#x8868;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;ChildProcess&#x5BF9;&#x8C61;</li>
<li>&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x540C;&#x6837;&#x62E5;&#x6709;stdin&#x5C5E;&#x6027;&#x503C;&#x4E3A;&#x4E00;&#x4E2A;&#x53EF;&#x7528;&#x4E8E;&#x8BFB;&#x5165;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x6D41;&#x5BF9;&#x8C61;</li>
<li>&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x540C;&#x6837;&#x62E5;&#x6709;stdiout&#x5C5E;&#x6027;&#x503C;&#x548C;stderr&#x5C5E;&#x6027;&#x503C;&#x53EF;&#x5206;&#x522B;&#x7528;&#x4E8E;&#x5199;&#x5165;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x6D41;&#x4E0E;&#x6807;&#x51C6;&#x9519;&#x8BEF;&#x8F93;&#x51FA;&#x6D41;</li>
</ul>
<h4 id="t172.1.2 close">2.1.2 close <a href="#t172.1.2 close"> # </a></h4>
<ul>
<li>&#x5F53;&#x5B50;&#x8FDB;&#x7A0B;&#x6240;&#x6709;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x90FD;&#x7EC8;&#x6B62;&#x65F6;&#xFF0C;&#x4F1A;&#x89E6;&#x53D1;&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;close&#x4E8B;&#x4EF6;&#x3002;<pre><code class="lang-javascript">child.on(<span class="hljs-string">&apos;close&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">code,signal</span>)</span>{});
</code></pre>
<ul>
<li>code &#x4E3A;0&#x8868;&#x793A;&#x6B63;&#x5E38;&#x63A8;&#x51FA;&#xFF0C;&#x4E3A;null&#x8868;&#x793A;&#x5F02;&#x5E38;&#x9000;&#x51FA;</li>
<li>&#x5F53;&#x5728;&#x7236;&#x8FDB;&#x7A0B;&#x4E2D;&#x5173;&#x95ED;&#x5B50;&#x8FDB;&#x7A0B;&#x65F6;&#xFF0C;signal&#x53C2;&#x6570;&#x503C;&#x4E3A;&#x7236;&#x8FDB;&#x7A0B;&#x53D1;&#x7ED9;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x4FE1;&#x53F7;&#x540D;&#x79F0;</li>
</ul>
</li>
</ul>
<h4 id="t182.1.3 exit">2.1.3 exit <a href="#t182.1.3 exit"> # </a></h4>
<ul>
<li>&#x5F53;&#x5B50;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#xFF0C;&#x89E6;&#x53D1;&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;exit&#x4E8B;&#x4EF6;</li>
<li><p>&#x56E0;&#x4E3A;&#x591A;&#x4E2A;&#x8FDB;&#x7A0B;&#x53EF;&#x80FD;&#x4F1A;&#x5171;&#x4EAB;i &#x4E2A;&#x8F93;&#x5165;/&#x8F93;&#x51FA;&#xFF0C;&#x6240;&#x4EE5;&#x5F53;&#x5B50;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x8F93;&#x5165;/&#x8F93;&#x51FA;&#x53EF;&#x80FD;&#x5E76;&#x672A;&#x7EC8;&#x6B62;</p>
<pre><code class="lang-javascript">child.on(<span class="hljs-string">&apos;exit&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">code,signal</span>)</span>{});
</code></pre>
<h4 id="t192.1.4 error">2.1.4 error <a href="#t192.1.4 error"> # </a></h4>
<p>&#x5982;&#x679C;&#x5B50;&#x8FDB;&#x7A0B;&#x5F00;&#x542F;&#x5931;&#x8D25;&#xFF0C;&#x90A3;&#x4E48;&#x5C06;&#x4F1A;&#x89E6;&#x53D1;&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;error&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascript">child1.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
  <span class="hljs-built_in">console</span>.log(err);
});
</code></pre>
<h4 id="t202.1.5 kill">2.1.5 kill <a href="#t202.1.5 kill"> # </a></h4>
</li>
<li>&#x7236;&#x8FDB;&#x7A0B;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;kill&#x65B9;&#x6CD5;&#x5411;&#x5B50;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x4FE1;&#x53F7;,&#x53C2;&#x6570;&#x4E3A;&#x63CF;&#x8FF0;&#x8BE5;&#x4FE1;&#x53F7;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x503C;&#x4E3A;<code>SIGTERM</code></li>
<li>SIGTERM &#x7A0B;&#x5E8F;&#x7ED3;&#x675F;(terminate)&#x4FE1;&#x53F7;, &#x4E0E;SIGKILL&#x4E0D;&#x540C;&#x7684;&#x662F;&#x8BE5;&#x4FE1;&#x53F7;&#x53EF;&#x4EE5;&#x88AB;&#x963B;&#x585E;&#x548C;&#x5904;&#x7406;. &#x901A;&#x5E38;&#x7528;&#x6765;&#x8981;&#x6C42;&#x7A0B;&#x5E8F;&#x81EA;&#x5DF1;&#x6B63;&#x5E38;&#x9000;&#x51FA;</li>
</ul>
<pre><code class="lang-javascript">child.kill([signal]);
</code></pre>
<h4 id="t212.1.6 &#x6848;&#x4F8B;">2.1.6 &#x6848;&#x4F8B; <a href="#t212.1.6 &#x6848;&#x4F8B;"> # </a></h4>
<ol>
<li>spawn.js<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> {
 spawn
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>);
<span class="hljs-comment">//&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;stdin,stdout,stderr&#x5BFC;&#x5411;&#x4E86;ChildProcess&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;child.stdin,child.stdout,child.stderr&#x6D41;,</span>
<span class="hljs-comment">//&#x8FD9;&#x548C;&#x8BBE;&#x7F6E;stdio&#x4E3A;[&apos;pipe&apos;, &apos;pipe&apos;, &apos;pipe&apos;]&#x662F;&#x4E00;&#x6837;&#x7684;</span>
<span class="hljs-keyword">let</span> p1 = spawn(<span class="hljs-string">&apos;node&apos;</span>, [<span class="hljs-string">&apos;test1.js&apos;</span>, <span class="hljs-string">&apos;a&apos;</span>], {
 <span class="hljs-attr">cwd</span>: path.join(__dirname, <span class="hljs-string">&apos;test1&apos;</span>)
});
<span class="hljs-keyword">let</span> p2 = spawn(<span class="hljs-string">&apos;node&apos;</span>, [<span class="hljs-string">&apos;test3.js&apos;</span>], {
 <span class="hljs-attr">cwd</span>: path.join(__dirname, <span class="hljs-string">&apos;test3&apos;</span>),
 <span class="hljs-attr">stdio</span>: <span class="hljs-string">&apos;pipe&apos;</span>
});
<span class="hljs-comment">//&#x76D1;&#x542C;test1.js&#x811A;&#x672C;&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x7684;data&#x4E8B;&#x4EF6;&#xFF0C;&#x628A;&#x6570;&#x636E;&#x5199;&#x7ED9;p2</span>
p1.stdout.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
 <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;P1:&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x51FA;:&apos;</span> + data);
 p2.stdin.write(data);
});
p1.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
 <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;p1:&#x5B50;&#x8FDB;&#x7A0B;1&#x5F00;&#x542F;&#x5931;&#x8D25;&apos;</span>);
});
p2.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
 <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;p2:&#x5B50;&#x8FDB;&#x7A0B;2&#x5F00;&#x542F;&#x5931;&#x8D25;&apos;</span>);
});
</code></pre>
</li>
<li><p>test1.js</p>
<pre><code class="lang-javascript">process.stdout.write(<span class="hljs-string">&apos;p1:&#x5B50;&#x8FDB;&#x7A0B;&#x5F53;&#x524D;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;&#x4E3A;:&apos;</span> + process.cwd() + <span class="hljs-string">&apos;\r\n&apos;</span>);
process.stdout.write(<span class="hljs-string">&apos;p1:&apos;</span> + process.argv[<span class="hljs-number">2</span>] + <span class="hljs-string">&apos; \r\n&apos;</span>);
</code></pre>
</li>
<li><p>test2.js</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> out = fs.createWriteStream(path.join(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>));
process.stdin.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
 out.write(data);
});
process.stdin.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
 process.exit();
});
</code></pre>
</li>
</ol>
<h4 id="t222.1.7 detached">2.1.7 detached <a href="#t222.1.7 detached"> # </a></h4>
<ul>
<li>&#x5728;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EA;&#x6709;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x5168;&#x90E8;&#x9000;&#x51FA;&#x540E;&#xFF0C;&#x7236;&#x8FDB;&#x7A0B;&#x624D;&#x80FD;&#x9000;&#x51FA;&#x3002;&#x4E3A;&#x4E86;&#x8BA9;&#x7236;&#x8FDB;&#x7A0B;&#x53EF;&#x4EE5;&#x5148;&#x9000;&#x51FA;&#xFF0C;&#x800C;&#x8BA9;&#x5B50;&#x8FDB;&#x7A0B;&#x7EE7;&#x7EED;&#x8FDB;&#x884C;I/O&#x64CD;&#x4F5C;,&#x53EF;&#x4EE5;&#x5728;spawn&#x65B9;&#x6CD5;&#x4E2D;&#x4F7F;&#x7528;options&#x53C2;&#x6570;&#xFF0C;&#x628A;detached&#x5C5E;&#x6027;&#x503C;&#x8BBE;&#x7F6E;&#x4E3A;true</li>
<li>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x7236;&#x8FDB;&#x7A0B;&#x4F1A;&#x7B49;&#x5F85;&#x6240;&#x6709;&#x7684;&#x5B50;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#x540E;&#x624D;&#x53EF;&#x4EE5;&#x9000;&#x51FA;&#xFF0C;&#x4F7F;&#x7528;subprocess.unref&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x8BA9;&#x7236;&#x8FDB;&#x7A0B;&#x4E0D;&#x7528;&#x7B49;&#x5F85;&#x5B50;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#x5C31;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x9000;&#x51FA;<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cp = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>);
<span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> out = fs.openSync(path.join(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>), <span class="hljs-string">&apos;w&apos;</span>, <span class="hljs-number">0o666</span>);
<span class="hljs-keyword">let</span> sp = cp.spawn(<span class="hljs-string">&apos;node&apos;</span>, [<span class="hljs-string">&apos;4.detached.js&apos;</span>], {
  <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">stdio</span>: [<span class="hljs-string">&apos;ignore&apos;</span>, out, <span class="hljs-string">&apos;ignore&apos;</span>]
});
sp.unref();
</code></pre>
</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> count = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> $timer = setInterval(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
    process.stdout.write(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().toString() + <span class="hljs-string">&apos;\r\n&apos;</span>);
    <span class="hljs-keyword">if</span> (--count == <span class="hljs-number">0</span>) {
        clearInterval($timer);
    }
}, <span class="hljs-number">500</span>);
<span class="hljs-string">`
</span></code></pre>
<h3 id="t232.2 fork&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;">2.2 fork&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B; <a href="#t232.2 fork&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;"> # </a></h3>
<ul>
<li>&#x884D;&#x751F;&#x4E00;&#x4E2A;&#x65B0;&#x7684; Node.js &#x8FDB;&#x7A0B;&#xFF0C;&#x5E76;&#x901A;&#x8FC7;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A; IPC &#x901A;&#x8BAF;&#x901A;&#x9053;&#x6765;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x8BE5;&#x901A;&#x9053;&#x5141;&#x8BB8;&#x7236;&#x8FDB;&#x7A0B;&#x4E0E;&#x5B50;&#x8FDB;&#x7A0B;&#x4E4B;&#x95F4;&#x76F8;&#x4E92;&#x53D1;&#x9001;&#x4FE1;&#x606F;</li>
<li>fork&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x9690;&#x5F0F;&#x521B;&#x5EFA;&#x7684;&#x4EE3;&#x8868;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;ChildProcess&#x5BF9;&#x8C61;</li>
<li>&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x8F93;&#x5165;/&#x8F93;&#x51FA;&#x64CD;&#x4F5C;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x540E;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x9000;&#x51FA;&#xFF0C;&#x5FC5;&#x987B;&#x4F7F;&#x7528;<code>process.exit()</code>&#x65B9;&#x6CD5;&#x663E;&#x5F0F;&#x9000;&#x51FA;</li>
</ul>
<pre><code class="lang-javascript">child_process.fork(modulePath,[args],[options]);
</code></pre>
<ul>
<li>args &#x8FD0;&#x884C;&#x8BE5;&#x6587;&#x4EF6;&#x6A21;&#x5757;&#x6587;&#x4EF6;&#x65F6;&#x8BB8;&#x54DF;&#x554A;&#x4F7F;&#x7528;&#x7684;&#x53C2;&#x6570;</li>
<li>options &#x9009;&#x9879;&#x5BF9;&#x8C61;<ul>
<li>cwd &#x6307;&#x5B9A;&#x5B50;&#x8FDB;&#x7A0B;&#x5F53;&#x524D;&#x7684;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;</li>
<li>env &#x5C5E;&#x6027;&#x503C;&#x4E3A;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x7528;&#x4E8E;&#x4EE5;&quot;&#x952E;&#x540D;/&#x952E;&#x503C;&quot;&#x7684;&#x5F62;&#x5F0F;&#x4E3A;&#x5B50;&#x8FDB;&#x7A0B;&#x6307;&#x5B9A;&#x73AF;&#x5883;&#x53D8;&#x91CF;</li>
<li>encoding &#x5C5E;&#x6027;&#x503C;&#x4E3A;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x8F93;&#x51FA;&#x53CA;&#x6807;&#x51C6;&#x9519;&#x8BEF;&#x8F93;&#x51FA;&#x6570;&#x636E;&#x7684;&#x7F16;&#x7801;&#x683C;&#x5F0F;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;&apos;utf8&apos;</li>
<li>silent &#x5C5E;&#x6027;&#x503C;&#x4E3A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x5F53;&#x5C5E;&#x6027;&#x503C;&#x4E3A;false&#x65F6;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x548C;&#x7236;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x5171;&#x4EAB;&#x6807;&#x51C6;(&#x8F93;&#x5165;/&#x8F93;&#x51FA;),true&#x65F6;&#x4E0D;&#x5171;&#x4EAB;</li>
</ul>
</li>
</ul>
<h4 id="t242.2.1 &#x53D1;&#x9001;&#x6D88;&#x606F;">2.2.1 &#x53D1;&#x9001;&#x6D88;&#x606F; <a href="#t242.2.1 &#x53D1;&#x9001;&#x6D88;&#x606F;"> # </a></h4>
<pre><code class="lang-javascript">child.send(message,[sendHandle]);<span class="hljs-comment">//&#x5728;&#x7236;&#x8FDB;&#x7A0B;&#x4E2D;&#x5411;&#x5B50;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x6D88;&#x606F;</span>
process.send(message,[sendHandle]);<span class="hljs-comment">//&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x5411;&#x4E3B;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x6D88;&#x606F;</span>
</code></pre>
<ul>
<li>message&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x9700;&#x8981;&#x53D1;&#x9001;&#x7684;&#x6D88;&#x606F;</li>
<li>sendHandle&#x662F;&#x4E00;&#x4E2A; net.Socket &#x6216; net.Server &#x5BF9;&#x8C61;</li>
<li>&#x5B50;&#x8FDB;&#x7A0B;&#x53EF;&#x4EE5;&#x76D1;&#x542C;&#x7236;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x7684;message&#x4E8B;&#x4EF6;<pre><code class="lang-javascript">process.on(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">m,setHandle</span>)</span>{});
</code></pre>
</li>
<li>m &#x53C2;&#x6570;&#x503C;&#x4E3A;&#x5B50;&#x8FDB;&#x7A0B;&#x6536;&#x5230;&#x7684;&#x6D88;&#x606F;</li>
<li>sendHandle&#x4E3A;&#x670D;&#x52A1;&#x5668;&#x5BF9;&#x8C61;&#x6216;socket&#x7AEF;&#x53E3;&#x5BF9;&#x8C61;</li>
</ul>
<p>&#x5F53;&#x7236;&#x8FDB;&#x7A0B;&#x6536;&#x5230;&#x5B50;&#x8FDB;&#x7A0B;&#x53D1;&#x51FA;&#x7684;&#x6D88;&#x606F;&#x65F6;&#xFF0C;&#x89E6;&#x53D1;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;message&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascript">child.on(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">m,setHandle</span>)</span>{
  <span class="hljs-comment">//TODO&#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4EE3;&#x7801;</span>
});
</code></pre>
<p>5.fork.js</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> {
    fork
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> child = fork(path.join(__dirname, <span class="hljs-string">&apos;fork.js&apos;</span>));
child.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">m</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7236;&#x8FDB;&#x7A0B;&#x63A5;&#x6536;&#x5230;&#x6D88;&#x606F;:&apos;</span>, m);
    process.exit();
});
child.send({
    <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>
});
child.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
    <span class="hljs-built_in">console</span>.error(<span class="hljs-built_in">arguments</span>);
});
</code></pre>
<p>fork.js</p>
<pre><code class="lang-javascript">process.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">m, setHandle</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x6536;&#x5230;&#x6D88;&#x606F;:&apos;</span>, m);
    process.send({
        <span class="hljs-attr">age</span>: <span class="hljs-number">9</span>
    });
})
</code></pre>
<h4 id="t252.2.2 silent">2.2.2 silent <a href="#t252.2.2 silent"> # </a></h4>
<p>&#x5728;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x4E0E;&#x7236;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x5171;&#x4EAB;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x548C;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x3002;&#x5982;&#x679C;&#x8981;&#x8BA9;&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x7528;&#x72EC;&#x7ACB;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;silent&#x5C5E;&#x6027;&#x503C;&#x8BBE;&#x7F6E;&#x4E3A; true
forkslient.js</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> {
    fork
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);

<span class="hljs-keyword">let</span> p1 = fork(<span class="hljs-string">&apos;node&apos;</span>, [path.join(__dirname, <span class="hljs-string">&apos;fork1.js&apos;</span>)], {
    <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>
});
<span class="hljs-keyword">let</span> p2 = fork(<span class="hljs-string">&apos;node&apos;</span>,path.join(__dirname, <span class="hljs-string">&apos;fork2.js&apos;</span>));
p1.stdout.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;1&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF1A;&apos;</span> + data);
    p2.send(data.toString());
});
p1.on(<span class="hljs-string">&apos;exit&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">code, signal</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#xFF0C;&#x9000;&#x51FA;&#x4EE3;&#x7801;&#x4E3A;:&apos;</span> + code);
});
p1.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x5F00;&#x542F;&#x5931;&#x8D25;:&apos;</span> + err);
    process.exit();
});
</code></pre>
<p>fork1.js</p>
<pre><code class="lang-javascript">process.argv.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) </span>{
    process.stdout.write(item + <span class="hljs-string">&apos;\r\n&apos;</span>);
});
</code></pre>
<p>fork2.js</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> out = fs.createWriteStream(path.join(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>));
process.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    out.write(data);
});
</code></pre>
<h4 id="t262.2.3 &#x5B50;&#x8FDB;&#x7A0B;&#x4E0E;&#x7236;&#x8FDB;&#x7A0B;&#x5171;&#x4EAB;HTTP&#x670D;&#x52A1;&#x5668;">2.2.3 &#x5B50;&#x8FDB;&#x7A0B;&#x4E0E;&#x7236;&#x8FDB;&#x7A0B;&#x5171;&#x4EAB;HTTP&#x670D;&#x52A1;&#x5668; <a href="#t262.2.3 &#x5B50;&#x8FDB;&#x7A0B;&#x4E0E;&#x7236;&#x8FDB;&#x7A0B;&#x5171;&#x4EAB;HTTP&#x670D;&#x52A1;&#x5668;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> {
    fork
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>);
<span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> child = fork(path.join(__dirname, <span class="hljs-string">&apos;8.child.js&apos;</span>));
<span class="hljs-keyword">let</span> server = net.createServer();
server.listen(<span class="hljs-number">8080</span>, <span class="hljs-string">&apos;127.0.0.1&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    child.send(<span class="hljs-string">&apos;server&apos;</span>, server);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7236;&#x8FDB;&#x7A0B;&#x4E2D;&#x7684;&#x670D;&#x52A1;&#x5668;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;&apos;</span>);
    <span class="hljs-keyword">let</span> httpServer = http.createServer();
    httpServer.on(<span class="hljs-string">&apos;request&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">if</span> (req.url != <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
            <span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">100000</span>; i++) {
                sum += <span class="hljs-number">1</span>;
            }
            res.write(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x5728;&#x7236;&#x8FDB;&#x7A0B;&#x4E2D;&#x88AB;&#x5904;&#x7406;&#x3002;&apos;</span>);
            res.end(<span class="hljs-string">&apos;sum=&apos;</span> + sum);
        }
    });
    httpServer.listen(server);
});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
process.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">msg, server</span>) </span>{
    <span class="hljs-keyword">if</span> (msg == <span class="hljs-string">&apos;server&apos;</span>) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x7684;&#x670D;&#x52A1;&#x5668;&#x5DF2;&#x7ECF;&#x88AB;&#x521B;&#x5EFA;&apos;</span>);
        <span class="hljs-keyword">let</span> httpServer = http.createServer();
        httpServer.on(<span class="hljs-string">&apos;request&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
            <span class="hljs-keyword">if</span> (req.url != <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
                sum = <span class="hljs-number">0</span>;
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10000</span>; i++) {
                    sum += i;
                }
                res.write(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x88AB;&#x5904;&#x7406;&apos;</span>);
                res.end(<span class="hljs-string">&apos;sum=&apos;</span> + sum);
            }
        });
        httpServer.listen(server);
    }
});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> options = {
    <span class="hljs-attr">hostname</span>: <span class="hljs-string">&apos;localhost&apos;</span>,
    <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
    <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
    <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;GET&apos;</span>
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
    <span class="hljs-keyword">let</span> req = http.request(options, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">res</span>) </span>{
        res.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">chunk</span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x54CD;&#x5E94;&#x5185;&#x5BB9;:&apos;</span> + chunk);
        });
    });
    req.end();
}
</code></pre>
<h4 id="t272.2.4 &#x5B50;&#x8FDB;&#x7A0B;&#x4E0E;&#x7236;&#x8FDB;&#x7A0B;&#x5171;&#x4EAB;socket&#x5BF9;&#x8C61;">2.2.4 &#x5B50;&#x8FDB;&#x7A0B;&#x4E0E;&#x7236;&#x8FDB;&#x7A0B;&#x5171;&#x4EAB;socket&#x5BF9;&#x8C61; <a href="#t272.2.4 &#x5B50;&#x8FDB;&#x7A0B;&#x4E0E;&#x7236;&#x8FDB;&#x7A0B;&#x5171;&#x4EAB;socket&#x5BF9;&#x8C61;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> {
    fork
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> child = fork(path.join(__dirname, <span class="hljs-string">&apos;11.socket.js&apos;</span>));
<span class="hljs-keyword">let</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>).createServer();
server.on(<span class="hljs-string">&apos;connection&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Date</span>.now() % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
        child.send(<span class="hljs-string">&apos;socket&apos;</span>, socket);
    } <span class="hljs-keyword">else</span> {
        socket.end(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x88AB;&#x7236;&#x8FDB;&#x7A0B;&#x5904;&#x7406;!&apos;</span>);
    }
});
server.listen(<span class="hljs-number">41234</span>, );
</code></pre>
<pre><code class="lang-javascript">process.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">m, socket</span>) </span>{
    <span class="hljs-keyword">if</span> (m === <span class="hljs-string">&apos;socket&apos;</span>) {
        socket.end(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x88AB;&#x5B50;&#x8FDB;&#x7A0B;&#x5904;&#x7406;.&apos;</span>);
    }
});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> client = <span class="hljs-keyword">new</span> net.Socket();
client.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
client.connect(<span class="hljs-number">41234</span>, <span class="hljs-string">&apos;localhost&apos;</span>);
client.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-built_in">console</span>.log(data);
});
</code></pre>
<h3 id="t282.3 exec&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;">2.3 exec&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B; <a href="#t282.3 exec&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;"> # </a></h3>
<ul>
<li>exec&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x8FD0;&#x884C;&#x67D0;&#x4E2A;&#x547D;&#x4EE4;&#x7684;&#x5B50;&#x8FDB;&#x7A0B;&#x5E76;&#x7F13;&#x5B58;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x8F93;&#x51FA;&#x7ED3;&#x679C;</li>
<li>spawn&#x662F;&#x4E00;&#x4E2A;&#x5F02;&#x6B65;&#x65B9;&#x6CD5;&#xFF0C;exec&#x662F;&#x4E00;&#x4E2A;&#x540C;&#x6B65;&#x65B9;&#x6CD5;</li>
<li>&#x884D;&#x751F;&#x4E00;&#x4E2A; shell &#x5E76;&#x5728; shell &#x4E0A;&#x8FD0;&#x884C;&#x547D;&#x4EE4;</li>
</ul>
<pre><code class="lang-javascript">child_process.exec(command,[options],[callback]);
</code></pre>
<ul>
<li>command &#x9700;&#x8981;&#x6267;&#x884C;&#x7684;&#x547D;&#x4EE4;</li>
<li>options &#x9009;&#x9879;&#x5BF9;&#x8C61;<ul>
<li>cwd &#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x5F53;&#x524D;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;</li>
<li>env &#x6307;&#x5B9A;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;</li>
<li>encoding &#x6307;&#x5B9A;&#x8F93;&#x51FA;&#x7684;&#x7F16;&#x7801;</li>
<li>timeout  &#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;</li>
<li>maxbuffer &#x6307;&#x5B9A;&#x7F13;&#x5B58;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x548C;&#x9519;&#x8BEF;&#x8F93;&#x51FA;&#x7684;&#x7F13;&#x5B58;&#x533A;&#x6700;&#x5927;&#x957F;&#x5EA6;</li>
<li>killSignal &#x6307;&#x5B9A;&#x5173;&#x95ED;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x4FE1;&#x53F7;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A; &quot;SIGTERM&quot;</li>
</ul>
</li>
<li>callback &#x6307;&#x5B9A;&#x5B50;&#x8FDB;&#x7A0B;&#x7EC8;&#x6B62;&#x65F6;&#x8C03;&#x7528;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,stdout,stderr</span>)</span>{}
</code></pre>
</li>
<li>err &#x9519;&#x8BEF;&#x5BF9;&#x8C61;</li>
<li>stdout &#x6807;&#x51C6;&#x8F93;&#x51FA;</li>
<li>stderr &#x9519;&#x8BEF;&#x8F93;&#x51FA;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> {
    exec
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> p1 = exec(<span class="hljs-string">&apos;node test1.js a b c&apos;</span>, {
    <span class="hljs-attr">cwd</span>: path.join(__dirname, <span class="hljs-string">&apos;test3&apos;</span>)
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, stdout, stderr</span>) </span>{
    <span class="hljs-keyword">if</span> (err) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x5F00;&#x542F;&#x5931;&#x8D25;:&apos;</span> + err);
        process.exit();
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x6807;&#x51C6;&#x8F93;&#x51FA;\r\n&apos;</span> + stdout.toString());
        p2.stdin.write(stdout.toString());
    }
});
<span class="hljs-keyword">let</span> p2 = exec(<span class="hljs-string">&apos;node test2.js&apos;</span>, {
    <span class="hljs-attr">cwd</span>: path.join(__dirname, <span class="hljs-string">&apos;test3&apos;</span>)
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, stdout, stderr</span>) </span>{
    process.exit();
});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
process.argv.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) </span>{
    process.stdout.write(item + <span class="hljs-string">&apos;\r\n&apos;</span>);
});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> out = fs.createWriteStream(path.join(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>));
process.stdin.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    out.write(data);
    process.exit();
})
</code></pre>
<h3 id="t292.4 execFile&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;">2.4 execFile&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B; <a href="#t292.4 execFile&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;"> # </a></h3>
<ul>
<li>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>execFile</code>&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x4E13;&#x95E8;&#x7528;&#x4E8E;&#x8FD0;&#x884C;&#x67D0;&#x4E2A;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x5B50;&#x8FDB;&#x7A0B;</li>
<li>&#x7C7B;&#x4F3C; child_process.exec()&#xFF0C;&#x4F46;&#x76F4;&#x63A5;&#x884D;&#x751F;&#x547D;&#x4EE4;&#xFF0C;&#x4E14;&#x65E0;&#x9700;&#x5148;&#x884D;&#x751F;&#x4E00;&#x4E2A; shell</li>
</ul>
<pre><code class="lang-javascript">child_process.execFile(file,[args],[optioins],[callback]);
</code></pre>
<ul>
<li>file &#x6307;&#x5B9A;&#x9700;&#x8981;&#x8FD0;&#x884C;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x53CA;&#x6587;&#x4EF6;&#x540D;</li>
<li>args &#x8FD0;&#x884C;&#x8BE5;&#x6587;&#x4EF6;&#x6240;&#x9700;&#x8981;&#x7684;&#x53C2;&#x6570;</li>
<li>options &#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x9009;&#x9879;</li>
<li>callback &#x6307;&#x5B9A;&#x5B50;&#x8FDB;&#x7A0B;&#x7EC8;&#x6B62;&#x65F6;&#x8C03;&#x7528;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> {
    execFile
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;child_process&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);

<span class="hljs-keyword">let</span> p1 = execFile(<span class="hljs-string">&apos;node&apos;</span>, [<span class="hljs-string">&apos;./test1.js&apos;</span>], {
    <span class="hljs-attr">cwd</span>: path.join(__dirname, <span class="hljs-string">&apos;test4&apos;</span>)
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, stdout, stderr</span>) </span>{
    <span class="hljs-keyword">if</span> (err) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;1&#x5F00;&#x542F;&#x5931;&#x8D25;:&apos;</span> + err);
        process.exit();
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x6807;&#x51C6;&#x8F93;&#x51FA;:&apos;</span> + stdout.toString());
        p2.stdin.write(stdout.toString());
    }
});
<span class="hljs-keyword">let</span> p2 = execFile(<span class="hljs-string">&apos;node&apos;</span>, [<span class="hljs-string">&apos;./test2.js&apos;</span>], {
    <span class="hljs-attr">cwd</span>: path.join(__dirname, <span class="hljs-string">&apos;test4&apos;</span>)
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, stdout, stderr</span>) </span>{
    <span class="hljs-keyword">if</span> (err) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;2&#x5F00;&#x542F;&#x5931;&#x8D25;:&apos;</span> + err);
        process.exit();
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x6807;&#x51C6;&#x8F93;&#x51FA;:&apos;</span> + stdout.toString());
    }
});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-meta">#!/usr/bin/env node</span>

<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
process.argv.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) </span>{
    process.stdout.write(item + <span class="hljs-string">&apos;\r\n&apos;</span>);
});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-meta">#!/usr/bin/env node</span>

<span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> out = fs.createWriteStream(path.join(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>));
process.stdin.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    out.write(data);
    process.exit();
})
</code></pre>
<h2 id="t303. cluster">3. cluster <a href="#t303. cluster"> # </a></h2>
<p>&#x4E3A;&#x4E86;&#x5229;&#x7528;&#x591A;&#x6838;CPU&#x7684;&#x4F18;&#x52BF;&#xFF0C;Node.js&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;cluster&#x6A21;&#x5757;&#x5141;&#x8BB8;&#x5728;&#x591A;&#x4E2A;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x8FD0;&#x884C;&#x4E0D;&#x540C;&#x7684;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x3002;</p>
<h3 id="t313.1 fork&#x65B9;&#x6CD5;&#x521B;&#x5EFA;work&#x5BF9;&#x8C61;">3.1 fork&#x65B9;&#x6CD5;&#x521B;&#x5EFA;work&#x5BF9;&#x8C61; <a href="#t313.1 fork&#x65B9;&#x6CD5;&#x521B;&#x5EFA;work&#x5BF9;&#x8C61;"> # </a></h3>
<ul>
<li>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;fork&#x65B9;&#x6CD5;&#x5F00;&#x542F;&#x591A;&#x4E2A;&#x5B50;&#x8FDB;&#x7A0B;&#xFF0C;&#x5728;&#x6BCF;&#x4E2A;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x8BE5;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x8FD0;&#x884C;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x6587;&#x4EF6;&#x3002;</li>
<li>fork&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x9690;&#x5F0F;&#x521B;&#x5EFA;&#x7684;work&#x5BF9;&#x8C61;</li>
<li>&#x5728;cluster&#x6A21;&#x5757;&#x4E2D;&#xFF0C;&#x5206;&#x522B;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;isMaster&#x5C5E;&#x6027;&#x4E0E;&#x4E00;&#x4E2A;isWork&#x5C5E;&#x6027;&#xFF0C;&#x90FD;&#x662F;&#x5E03;&#x5C14;&#x503C;<pre><code class="lang-javascript">cluster.fork([env]);
</code></pre>
</li>
<li>env &#x4E3A;&#x5B50;&#x8FDB;&#x7A0B;&#x6307;&#x5B9A;&#x73AF;&#x5883;&#x53D8;&#x91CF;</li>
</ul>
<h4 id="t323.1.1 &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;worker">3.1.1 &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;worker <a href="#t323.1.1 &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;worker"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> index <span class="hljs-keyword">in</span> cluster.workers){
    <span class="hljs-built_in">console</span>.log(cluster.workers[index]);
}
</code></pre>
<h4 id="t333.1.2 &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;worker&#x548C;id">3.1.2 &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;worker&#x548C;id <a href="#t333.1.2 &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;worker&#x548C;id"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(cluster.isMaster){
  cluster.fork()
}<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(cluster.isWorker){
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;I am worker #&apos;</span>+cluster.worker.id);
}
</code></pre>
<h4 id="t343.1.3 &#x670D;&#x52A1;&#x5668;">3.1.3 &#x670D;&#x52A1;&#x5668; <a href="#t343.1.3 &#x670D;&#x52A1;&#x5668;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">if</span> (cluster.isMaster) {
    cluster.fork();
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x5728;&#x4E3B;&#x8FDB;&#x7A0B;&#x91CC;&apos;</span>);
} <span class="hljs-keyword">else</span> {
    http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">if</span> (req.url != <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
            res.end(<span class="hljs-string">&apos;hello&apos;</span>);
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x91CC;&apos;</span>);
        }
    }).listen(<span class="hljs-number">8080</span>);
}
</code></pre>
<h4 id="t353.1.4 fork&#x4E8B;&#x4EF6;">3.1.4 fork&#x4E8B;&#x4EF6; <a href="#t353.1.4 fork&#x4E8B;&#x4EF6;"> # </a></h4>
<p>&#x5F53;&#x4F7F;&#x7528;fork&#x65B9;&#x6CD5;&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;&#x65F6;&#xFF0C;&#x5C06;&#x540C;&#x65F6;&#x89E6;&#x53D1;fork&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascript">cluster.on(<span class="hljs-string">&apos;fork&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">worker</span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B; &apos;</span>+workder.id+<span class="hljs-string">&apos;&#x88AB;&#x5F00;&#x542F;&apos;</span>);  
});
</code></pre>
<h4 id="t363.1.5 online&#x4E8B;&#x4EF6;">3.1.5 online&#x4E8B;&#x4EF6; <a href="#t363.1.5 online&#x4E8B;&#x4EF6;"> # </a></h4>
<ul>
<li>&#x5728;&#x4F7F;&#x7528;fork&#x65B9;&#x6CD5;&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7528;&#x4E8E;&#x8FD0;&#x884C;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x5B50;&#x8FDB;&#x7A0B;&#x540E;&#xFF0C;&#x8BE5;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x5C06;&#x901A;&#x8FC7;&#x5411;&#x4E3B;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x53CD;&#x9988;&#x4FE1;&#x606F;&#xFF0C;&#x5F53;&#x4E3B;&#x8FDB;&#x7A0B;&#x63A5;&#x6536;&#x5230;&#x8BE5;&#x53CD;&#x9988;&#x4FE1;&#x606F;&#x540E;&#xFF0C;&#x89E6;&#x53D1;online&#x4E8B;&#x4EF6;</li>
</ul>
<pre><code class="lang-javascript">cluster.on(<span class="hljs-string">&apos;online&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">worker</span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5DF2;&#x7ECF;&#x6536;&#x5230;&#x5B50;&#x8FDB;&#x7A0B;#&apos;</span>+workder.id+<span class="hljs-string">&quot;&#x7684;&#x6D88;&#x606F;&quot;</span>);
});
</code></pre>
<h4 id="t373.1.6 listening">3.1.6 listening <a href="#t373.1.6 listening"> # </a></h4>
<p>&#x5F53;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x8FD0;&#x884C;&#x7684;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x8C03;&#x7528;&#x670D;&#x52A1;&#x5668;&#x7684;listen&#x65B9;&#x6CD5;&#x540E;&#xFF0C;&#x8BE5;&#x670D;&#x52A1;&#x5668;&#x5F00;&#x59CB;&#x5BF9;&#x6307;&#x5B9A;&#x5730;&#x5740;&#x53CA;&#x7AEF;&#x53E3;&#x8FDB;&#x884C;&#x76D1;&#x542C;&#xFF0C;&#x540C;&#x65F6;&#x89E6;&#x53D1;listening&#x4E8B;&#x4EF6;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">if</span> (cluster.isMaster) {
    cluster.fork();
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x5728;&#x4E3B;&#x8FDB;&#x7A0B;&#x91CC;&apos;</span>);
} <span class="hljs-keyword">else</span> {
    http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">if</span> (req.url != <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
            res.end(<span class="hljs-string">&apos;hello&apos;</span>);
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x91CC;&apos;</span>);
        }
    }).listen(<span class="hljs-number">8080</span>, <span class="hljs-string">&apos;localhost&apos;</span>);
}
cluster.on(<span class="hljs-string">&apos;online&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">worker</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5DF2;&#x7ECF;&#x6536;&#x5230;&#x5B50;&#x8FDB;&#x7A0B;#&apos;</span> + worker.id + <span class="hljs-string">&quot;&#x7684;&#x6D88;&#x606F;&quot;</span>);
});
cluster.on(<span class="hljs-string">&apos;listening&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">worker, address</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x7684;&#x670D;&#x52A1;&#x5668;&#x5F00;&#x59CB;&#x76D1;&#x542C;,&#x5730;&#x5740;&#x4E3A;:&apos;</span> + address.address + <span class="hljs-string">&quot;:&quot;</span> + address.port);
});
</code></pre>
<h4 id="t383.1.7 setupMaster">3.1.7 setupMaster <a href="#t383.1.7 setupMaster"> # </a></h4>
<p>&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x7684;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x9ED8;&#x8BA4;&#x8FD0;&#x884C;&#x5F53;&#x524D;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x7684;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x7684;&#x4E3B;&#x6A21;&#x5757;&#x6587;&#x4EF6;&#x3002;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;setupMaster&#x65B9;&#x6CD5;&#x4FEE;&#x6539;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x8FD0;&#x884C;&#x7684;&#x6A21;&#x5757;&#x6587;&#x4EF6;</p>
<pre><code class="lang-javascript">cluster.setupMaster([settings]);
</code></pre>
<ul>
<li>settings&#x8BBE;&#x7F6E;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x8FD0;&#x884C;&#x7684;Node.js&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x5404;&#x79CD;&#x9ED8;&#x8BA4;&#x884C;&#x4E3A;&#x7684;&#x5BF9;&#x8C61;<ul>
<li>exec &#x5B50;&#x8FDB;&#x7A0B;&#x8FD0;&#x884C;&#x7684;&#x6A21;&#x5757;&#x6587;&#x4EF6;&#x540D;&#x79F0;&#x7684;&#x5B8C;&#x6574;&#x8DEF;&#x5F84;&#x53CA;&#x6587;&#x4EF6;&#x540D;</li>
<li>args &#x5C5E;&#x6027;&#x4E3A;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x5176;&#x4E2D;&#x5B58;&#x653E;&#x4E86;&#x6240;&#x6709;&#x8FD0;&#x884C;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;Node.js&#x8FD0;&#x884C;&#x7A0B;&#x5E8F;&#x6240;&#x9700;&#x8981;&#x7684;&#x53C2;&#x6570;</li>
<li>silent &#x5E03;&#x5C14;&#x503C;&#x3002;&#x5F53;&#x5C5E;&#x6027;&#x503C;&#x4E3A;false&#x65F6;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x4E0E;&#x4E3B;&#x8FDB;&#x7A0B;&#x5BF9;&#x8C61;&#x5171;&#x4EAB;&#x6807;&#x51C6;&#x8F93;&#x5165;/&#x8F93;&#x51FA;</li>
</ul>
</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
cluster.setupMaster({
    <span class="hljs-attr">exec</span>: <span class="hljs-string">&apos;subtask.js&apos;</span>
});
cluster.fork();
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x88AB;&#x8FD0;&#x884C;&#x5B50;&#x4E3B;&#x8FDB;&#x7A0B;&#x4E2D;&apos;</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;cluster.settings&#x5C5E;&#x6027;&#x503C;: %j&apos;</span>, cluster.settings);
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    <span class="hljs-keyword">if</span> (req.url != <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
        res.writeHead(<span class="hljs-number">200</span>);
        res.end(<span class="hljs-string">&apos;ok&apos;</span>);
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x88AB;&#x8FD0;&#x884C;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&apos;</span>);
    }
}).listen(<span class="hljs-number">8080</span>);
</code></pre>
<h4 id="t393.1.8 &#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x91CC;&#x8FD0;&#x884C;&#x670D;&#x52A1;&#x5668;">3.1.8 &#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x91CC;&#x8FD0;&#x884C;&#x670D;&#x52A1;&#x5668; <a href="#t393.1.8 &#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x91CC;&#x8FD0;&#x884C;&#x670D;&#x52A1;&#x5668;"> # </a></h4>
<p>&#x5F53;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x91CC;&#x8FD0;&#x884C;&#x670D;&#x52A1;&#x5668;&#x65F6;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x603B;&#x662F;&#x5148;&#x88AB;&#x4E3B;&#x8FDB;&#x7A0B;&#x63A5;&#x6536;&#xFF0C;&#x7136;&#x540E;&#x8F6C;&#x53D1;&#x7ED9;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x7684;&#x670D;&#x52A1;&#x5668;&#x3002;&#x5982;&#x679C;&#x5728;&#x591A;&#x4E2A;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x8FD0;&#x884C;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5F53;&#x4E3B;&#x8FDB;&#x7A0B;&#x63A5;&#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x540E;&#xFF0C;&#x5C06;&#x4F1A;&#x81EA;&#x52A8;&#x5206;&#x914D;&#x7ED9;&#x4E00;&#x4E2A;&#x5F53;&#x524D;&#x5904;&#x4E8E;&#x7A7A;&#x95F2;&#x72B6;&#x6001;&#x7684;&#x5B50;&#x8FDB;&#x7A0B;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">if</span> (cluster.isMaster) {
    cluster.fork();
    cluster.fork();
} <span class="hljs-keyword">else</span> {
    http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">if</span> (req.url !== <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
            <span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000000</span>; i++) {
                sum += i;
            }
            res.writeHead(<span class="hljs-number">200</span>);
            res.write(<span class="hljs-string">`&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${cluster.worker.id}</span>&#x4E2D;&#x88AB;&#x5904;&#x7406;`</span>);
            res.end(<span class="hljs-string">`&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${cluster.worker.id}</span>&#x4E2D;&#x7684;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;=<span class="hljs-subst">${sum}</span>`</span>);
        }
    }).listen(<span class="hljs-number">8080</span>);
}
</code></pre>
<h4 id="t403.1.9 &#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4F7F;&#x7528;&#x5355;&#x72EC;&#x7684;&#x8F93;&#x51FA;">3.1.9 &#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4F7F;&#x7528;&#x5355;&#x72EC;&#x7684;&#x8F93;&#x51FA; <a href="#t403.1.9 &#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4F7F;&#x7528;&#x5355;&#x72EC;&#x7684;&#x8F93;&#x51FA;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">if</span> (cluster.isMaster) {
    cluster.setupMaster({
        <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>
    });
    <span class="hljs-keyword">let</span> worker = cluster.fork();
    worker.process.stdout.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x63A5;&#x6536;&#x5230;&#x6765;&#x81EA;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x76EE;&#x6807;&#x5730;&#x5740;:&apos;</span> + data);
    });
} <span class="hljs-keyword">else</span> {
    http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">if</span> (req.url !== <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
            <span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000000</span>; i++) {
                sum += i;
            }
            res.writeHead(<span class="hljs-number">200</span>);
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${cluster.worker.id}</span>&#x4E2D;&#x88AB;&#x5904;&#x7406;`</span>);
            res.write(<span class="hljs-string">`&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${cluster.worker.id}</span>&#x4E2D;&#x88AB;&#x5904;&#x7406;`</span>);
            res.end(<span class="hljs-string">`&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${cluster.worker.id}</span>&#x4E2D;&#x7684;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;=<span class="hljs-subst">${sum}</span>`</span>);
        }
    }).listen(<span class="hljs-number">8080</span>);
}
</code></pre>
<h3 id="t413.2 worker&#x5BF9;&#x8C61;">3.2 worker&#x5BF9;&#x8C61; <a href="#t413.2 worker&#x5BF9;&#x8C61;"> # </a></h3>
<h4 id="t423.2.1 online">3.2.1 online <a href="#t423.2.1 online"> # </a></h4>
<p>&#x5F53;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x8FDB;&#x7A0B;&#x540E;&#xFF0C;&#x5DE5;&#x4F5C;&#x8FDB;&#x7A0B;&#x5E94;&#x5F53;&#x54CD;&#x5E94;&#x4E00;&#x4E2A;online&#x6D88;&#x606F;&#x7ED9;&#x4E3B;&#x8FDB;&#x7A0B;&#x3002;&#x5F53;&#x4E3B;&#x8FDB;&#x7A0B;&#x6536;&#x5230;online&#x6D88;&#x606F;&#x540E;&#x89E6;&#x53D1;&#x8FD9;&#x4E2A;&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">if</span> (cluster.isMaster) {
    <span class="hljs-keyword">let</span> worker = cluster.fork();
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x88AB;&#x8FD0;&#x884C;&#x5728;&#x4E3B;&#x8FDB;&#x7A0B;&#x91CC;&apos;</span>);
    worker.on(<span class="hljs-string">&apos;online&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5DF2;&#x7ECF;&#x6536;&#x5230;&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${worker.id}</span>&#x7684;&#x8FD0;&#x884C;&#x4FE1;&#x606F;`</span>);
    });
} <span class="hljs-keyword">else</span> {
    http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">if</span> (req.url !== <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
            <span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000000</span>; i++) {
                sum += i;
            }
            res.end(<span class="hljs-string">&apos;ok&apos;</span>);
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x88AB;&#x8FD0;&#x884C;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x3002;`</span>);
        }
    }).listen(<span class="hljs-number">8080</span>);
}
</code></pre>
<h4 id="t433.2.2 send">3.2.2 send <a href="#t433.2.2 send"> # </a></h4>
<p>&#x5728;&#x4F7F;&#x7528;fork&#x53D1;&#x653E;&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;fork&#x65B9;&#x6CD5;&#x6240;&#x8FD4;&#x56DE;&#x7684;worker&#x5BF9;&#x8C61;&#x7684;send&#x65B9;&#x6CD5;&#x5728;&#x4E3B;&#x8FDB;&#x7A0B;&#x4E2D;&#x5411;&#x5B50;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x3002;</p>
<pre><code class="lang-javascript">worker.send(message,[sendHandle]);<span class="hljs-comment">//&#x5728;&#x4E3B;&#x8FDB;&#x7A0B;&#x4E2D;&#x5411;&#x5B50;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x6D88;&#x606F;</span>
process.send(message,[sendHandle]);<span class="hljs-comment">//&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x50CF;&#x4E3B;&#x8FDB;&#x7A0B;&#x53D1;&#x9001;&#x6D88;&#x606F;</span>
process.on(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">m,setHandle</span>)</span>{});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
cluster.setupMaster({
    <span class="hljs-attr">exec</span>: <span class="hljs-string">&apos;child.js&apos;</span>
});
<span class="hljs-keyword">let</span> worker = cluster.fork();
worker.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">m</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7236;&#x8FDB;&#x7A0B;&#x63A5;&#x6536;&#x5230;&#x6D88;&#x606F;:&apos;</span>, m);
    process.exit();
});
worker.send({
    <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>
});
</code></pre>
<h4 id="t443.2.3 &#x5171;&#x4EAB;socket">3.2.3 &#x5171;&#x4EAB;socket <a href="#t443.2.3 &#x5171;&#x4EAB;socket"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
<span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
cluster.setupMaster({
    <span class="hljs-attr">exec</span>: <span class="hljs-string">&apos;22.subsocket.js&apos;</span>
});
<span class="hljs-keyword">let</span> worker = cluster.fork();
<span class="hljs-keyword">let</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>).createServer();
server.on(<span class="hljs-string">&apos;connection&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Date</span>.now() % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
        worker.send(<span class="hljs-string">&apos;socket&apos;</span>, socket);
    } <span class="hljs-keyword">else</span> {
        socket.end(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;&#x5728;&#x4E3B;&#x8FDB;&#x7A0B;&#x4E2D;&#x5904;&#x7406;&apos;</span>);
    }

});
server.listen(<span class="hljs-number">41234</span>, <span class="hljs-string">&apos;localhost&apos;</span>);
worker.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">m, socket</span>) </span>{
    <span class="hljs-built_in">console</span>.log(m);

});
</code></pre>
<pre><code class="lang-javascript">process.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">msg, socket</span>) </span>{
    <span class="hljs-keyword">if</span> (msg == <span class="hljs-string">&apos;socket&apos;</span>) {
        socket.end(<span class="hljs-string">&apos;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#x8FD4;&#x56DE;&#x6D88;&#x606F;:&apos;</span> + msg);
        process.send(<span class="hljs-string">&apos;&#x544A;&#x8BC9;&#x7236;&#x8FDB;&#x7A0B;&#x6211;&#x5904;&#x7406;&#x4E86;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&apos;</span>);
    }
});
</code></pre>
<h4 id="t453.2.4 kill">3.2.4 kill <a href="#t453.2.4 kill"> # </a></h4>
<p>&#x5F53;&#x4F7F;&#x7528;fork&#x65B9;&#x6CD5;&#x5F00;&#x542F;&#x5B50;&#x8FDB;&#x7A0B;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;fork&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;worker&#x5BF9;&#x8C61;&#x7684;kill&#x65B9;&#x6CD5;&#x5F3A;&#x5236;&#x5173;&#x95ED;&#x5B50;&#x8FDB;&#x7A0B;</p>
<pre><code class="lang-javascript">worker.kill([signal]);
</code></pre>
<ul>
<li>signal &#x5F3A;&#x5236;&#x5173;&#x95ED;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x4FE1;&#x53F7;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x503C;&#x4E3A; &quot;SIGTERM&quot;</li>
</ul>
<h4 id="t463.2.5 exit">3.2.5 exit <a href="#t463.2.5 exit"> # </a></h4>
<p>&#x5F53;&#x5B50;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#xFF0C;&#x5C06;&#x4F1A;&#x89E6;&#x53D1;worker&#x5BF9;&#x8C61;&#x7684;exit&#x4E8B;&#x4EF6;</p>
<pre><code>worker.on(&apos;exit&apos;,function(code,signal));
</code></pre><ul>
<li>code &#x9000;&#x51FA;&#x4EE3;&#x7801;&#x3002;&#x6B63;&#x5E38;&#x9000;&#x51FA;&#x4E3A;0&#xFF0C;&#x5F02;&#x5E38;&#x9000;&#x51FA;&#x4E3A;null</li>
<li>worker.exitedAfterDisconnect&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x533A;&#x5206;&#x81EA;&#x53D1;&#x9000;&#x51FA;&#x8FD8;&#x662F;&#x88AB;&#x52A8;&#x9000;&#x51FA;&#xFF0C;&#x4E3B;&#x8FDB;&#x7A0B;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x503C;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x91CD;&#x65B0;&#x884D;&#x751F;&#x65B0;&#x7684;&#x5DE5;&#x4F5C;&#x8FDB;&#x7A0B;&#x3002;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">if</span> (cluster.isMaster) {
    cluster.setupMaster({
        <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>
    });
    <span class="hljs-keyword">let</span> worker = cluster.fork();
    worker.process.stdout.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x63A5;&#x6536;&#x5230;&#x6765;&#x81EA;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;,&#x76EE;&#x6807;&#x5730;&#x5740;&#x4E3A;:&apos;</span> + data);
        worker.kill(); <span class="hljs-comment">//&#x8BA9;&#x5B50;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;</span>
    });
    worker.on(<span class="hljs-string">&apos;exit&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">code, signal</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>)
        <span class="hljs-comment">//suicide&#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x662F;&#x81EA;&#x52A8;&#x9000;&#x51FA;&#x8FD8;&#x662F;&#x5F02;&#x5E38;&#x9000;&#x51FA;&#xFF0C;&#x9000;&#x51FA;&#x4E4B;&#x524D;&#x65F6;undefined.&#x81EA;&#x52A8;&#x9000;&#x51FA;&#x4E3A;true,&#x5F02;&#x5E38;&#x9000;&#x51FA;&#x4E3A;false</span>
        <span class="hljs-keyword">if</span> (worker.exitedAfterDisconnect  == <span class="hljs-literal">true</span>) {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5B50;&#x8FDB;&#x7A0B;%{worker.id}&#x81EA;&#x52A8;&#x9000;&#x51FA;`</span>);
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (worker.exitedAfterDisconnect  == <span class="hljs-literal">false</span>) {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${worker.id}</span>&#x5F02;&#x5E38;&#x9000;&#x51FA;&#xFF0C;&#x9000;&#x51FA;&#x4EE3;&#x7801;&#x4E3A;<span class="hljs-subst">${code}</span>`</span>);
        }
        <span class="hljs-keyword">if</span> (signal) {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x9000;&#x51FA;&#x4FE1;&#x53F7;&#x4E3A; %s&apos;</span>, signal);
        }
    });
} <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">let</span> server = http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">if</span> (req.url != <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
            res.end(<span class="hljs-string">&apos;hello&apos;</span>);
            process.stdout.write(req.url);
        }
    }).listen(<span class="hljs-number">8080</span>);
}
</code></pre>
<h4 id="t473.2.6 disconnect">3.2.6 disconnect <a href="#t473.2.6 disconnect"> # </a></h4>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;worker&#x5BF9;&#x8C61;&#x7684;disconnect&#x65B9;&#x6CD5;&#x4F7F;&#x8BE5;&#x5B50;&#x8FDB;&#x7A0B;&#x4E0D;&#x518D;&#x63A5;&#x6536;&#x5916;&#x90E8;&#x8FDE;&#x63A5;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cluster&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">if</span> (cluster.isMaster) {
    cluster.setupMaster({ <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span> });
    <span class="hljs-keyword">let</span> worker = cluster.fork();
    worker.process.stdout.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x63A5;&#x6536;&#x5230;&#x6765;&#x81EA;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;,&#x76EE;&#x6807;&#x5730;&#x5740;&#x4E3A;:&apos;</span> + data);
        setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            worker.disconnect();<span class="hljs-comment">//&#x4E0D;&#x518D;&#x63A5;&#x6536;&#x8FDE;&#x63A5;</span>
            worker.send(<span class="hljs-string">&apos;disconnect&apos;</span>);
        }, <span class="hljs-number">2000</span>);
    });

    worker.on(<span class="hljs-string">&apos;disconnect&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${worker.id}</span>&#x65AD;&#x5F00;&#x8FDE;&#x63A5;`</span>);
    });
    worker.on(<span class="hljs-string">&apos;exit&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">code, signal</span>) </span>{
        <span class="hljs-keyword">if</span> (worker.exitedAfterDisconnect) {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${worker.id}</span>&#x6B63;&#x5E38;&#x9000;&#x51FA;`</span>);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5B50;&#x8FDB;&#x7A0B;<span class="hljs-subst">${worker.id}</span>&#x5F02;&#x5E38;&#x9000;&#x51FA;`</span>);
        }
        <span class="hljs-keyword">if</span> (signal) {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos; &#x9000;&#x51FA;&#x4FE1;&#x53F7;&#x4E3A;&apos;</span> + signal);
        }
    });

} <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
    <span class="hljs-keyword">let</span> server = http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">if</span> (req.url != <span class="hljs-string">&apos;/favicon.ico&apos;</span>) {
            res.end(<span class="hljs-string">&apos;hello&apos;</span>);
            process.stdout.write(req.url);
        }
    }).listen(<span class="hljs-number">8080</span>);
    process.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">msg</span>) </span>{
        <span class="hljs-keyword">if</span> (msg == <span class="hljs-string">&apos;disconnect&apos;</span>) {
            <span class="hljs-comment">//process.exit(0);</span>
        }

    });
}

</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
